﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework
 * Version 2.10
 * 
 * CNR Command Parameter Class
 *      Provides a base definition for implementing command parameters in
 * a command-and-response protocol.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using Adaptive.Foundation;

namespace Adaptive.Foundation.Network.Cnr
{
    /// <summary>
    /// Provides a base definition for implementing command parameters in
    /// a command-and-response protocol.
    /// </summary>
    /// <remarks>
    /// This base class is intended to be extended for the specific application within a 
    /// particular command-and-response protocol, such as FTP, E/SMTP, POP3, and others.
    /// </remarks>
    public class CnrCommandParameter<T> : CnrCommandParameterBase 
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// The actual content for the data parameter.
        /// </summary>
        private T _dataContent;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="parameterName">
        /// The name assigned to the parameter.
        /// </param>
        public CnrCommandParameter(string parameterName)
            : base(parameterName)
        {
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="parameterName">
        /// The name assigned to the parameter.
        /// </param>
        /// <param name="dataContent">
        /// The data assigned to the parameter instance.
        /// </param>
        public CnrCommandParameter(string parameterName, T dataContent)
            : base(parameterName)
        {
            _dataContent = dataContent;
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
                _dataContent = default(T);
            }
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets the value of the data content for the parameter.
        /// </summary>
        /// <value>
        /// The data value for the command parameter.
        /// </value>
        public T DataContent
        {
            get { return _dataContent; }
            set { _dataContent = value; }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Renders the parameter value.
        /// </summary>
        /// <returns>
        /// A string containing the parameter data.
        /// </returns>
        protected override string RenderParameter()
        {
            return string.Empty;
        }
        /// <summary>
        /// Renders the parameter value.
        /// </summary>
        /// <returns>
        /// A string containing the parameter data.
        /// </returns>
        protected override string RenderParameter(CnrDataRendererBase renderer)
        {
            string returnValue = string.Empty;

            try
            {
                returnValue = renderer.RenderParameter(this);
            }
            catch (Exception ex)
            {
                OnRenderingException(new ExceptionEventArgs(ex));
            }
            return returnValue;
        }
        /// <summary>
        /// Renders the parameter value.
        /// </summary>
        /// <returns>
        /// A string containing the parameter data.
        /// </returns>
        protected override void ParseParameter(CnrDataParserBase parser, string originalData)
        {
            try
            {
                Name = parser.GetParameterName(originalData);
                _dataContent = (T)parser.GetParameterValue(originalData);
            }
            catch (Exception ex)
            {
                OnParsingException(new ExceptionEventArgs(ex));
            }
        }
        /// <summary>
        /// Validates the parameter definition and content.
        /// </summary>
        /// <returns>
        /// Returns <b>false</b>.
        /// </returns>
        protected override bool ValidateParameter()
        {
            return false;
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Clears the contents of the object instance without disposing of the instance.
        /// </summary>
        public override void Clear()
        {
            _dataContent = default(T);
        }
        #endregion
    }
}
